
if ("undefined" == typeof bugs) bugs = {};

bugs.login = {
	initHome : function()	{
		bugs.login.init("frmLoginHome",
			jQuery("#frmLoginHome button.btnBugsLogin"), jQuery("#frmLoginHome span input"),
			"text", "text", "focus", "focus", "");
	},
	initGnb : function() {
		bugs.login.init("frmLoginGnb", 
			jQuery("#frmLoginGnb button.btnLogin"), jQuery("p input"), 
			"inputID", "inputPW", "focus", "focus", "endInput");
		bugs.login.initLogged();
	},
	initPopup : function() {
		bugs.login.init("frmLoginPopup", 
			jQuery("#frmLoginPopup button.btnLogin"), jQuery(".enterIDPW input"), 
			"default", "default", "focus", "focus", "focus");
	},
	initCp : function() {
		bugs.login.init("frmLoginCp", 
			jQuery("#frmLoginCp button.btnLogin"), jQuery("p input"), 
			"inputID", "inputPW", "focus", "focus", "endInput");
	},
	initLogged : function() {
		jQuery(".myInfo .layerMyInfo .infoB .cash .btn").click(buyCash);
	}, 
	init : function(formId, submitButton, inputs, 
			defaultInputClass, defaultPasswdClass, focusInputClass, focusPasswdClass, endInputClass) {
		
		var _form  = jQuery("#" + formId);
		var userId = jQuery("input[name=user_id]", _form);
		//if(!bugs.login.empty(userId.val())) userId.addClass(endInputClass);
	
		var passWd = jQuery("input[name=passwd]", _form);;
		//if(!bugs.login.empty(passWd.val())) passWd.addClass(endInputClass);
		
		
		passWd.keypress(function(event) {
			if(event.keyCode == 13) {
				bugs.login.submit(_form, userId, passWd);
				return false; 
			} 
		}).keyup(function(event) {
			if (passWd.val().length==0) jQuery('#label_pw').show();
			else jQuery('#label_pw').hide();
		}).blur(function(event) {
			if (passWd.val().length==0) jQuery('#label_pw').show();
		});
		
		userId.keypress(function(event) {
			if(event.keyCode == 13) {
				bugs.login.submit(_form, userId, passWd);
				return false; 
			} 
		})
		.keyup(function(event) {
			if (userId.val().length==0) jQuery('#label_id').show();
			else jQuery('#label_id').hide();
		}).blur(function(event) {
			if (userId.val().length==0) jQuery('#label_id').show();
		});
		
			inputs.each(function(idx) {
				jQuery(this).bind("focus", function(event) {
					var className;
					if(idx == 0) {
						jQuery(event.target).removeClass(endInputClass);
						jQuery(event.target).addClass(focusInputClass);
					} else if(idx == 1) {
						jQuery(event.target).removeClass(endInputClass);
						jQuery(event.target).addClass(focusPasswdClass);
					}
				}).bind("blur", function(event) {
					//if(!bugs.login.empty(this.value)) {
					//	jQuery(event.target).addClass(endInputClass);
					//} else {
						if(idx == 0) {
							jQuery(event.target).removeClass(focusInputClass);							
							jQuery(event.target).addClass(defaultInputClass);
						} else if(idx == 1) {
							jQuery(event.target).removeClass(focusPasswdClass);
							jQuery(event.target).addClass(defaultPasswdClass);
						}
					//}
				});
			});
		
		var saveId = bugs.cookie.get('saveId');
		if (!bugs.valid.empty(saveId)) {
			userId.val(saveId);
			jQuery("#label_id").hide();
			jQuery("#keepUid").attr("checked","checked");
		}
		
		/*
		if(jQuery("#keepUid", _form).length > 0) {
			jQuery("#keepUid", _form).click(function() {
				if (jQuery(this).is(":checked")) {
					bugs.login.alert("<p><strong>현재 사용하시는 PC에<br />아이디를 저장하시겠습니까?</strong></p>" +
							"<p>PC방 등 공공장소에서는 개인정보가<br />유출될 수있으니 주의해 주시기 바랍니다.</p>", 
							{css : "layerLoginMsg"});
				}
			});
		}
		*/
		/*
		if(jQuery("#autoLogin", _form).length > 0) {
			jQuery("#autoLogin", _form).click(function() {
				if (jQuery(this).is(":checked")) {
					var show = bugs.cookie.get('autoLogin');
					if (show != "Y") {
						bugs.layermenu.showClickLayer('.layerAutoLogin');
					}
				} else {
					jQuery('.layerAutoLogin').hide();
				}
			});
			jQuery("#layerOpenCheckAotoLogin", _form).click(function() {
				var oDate = new Date();
				oDate.setDate(oDate.getDate()+365);
				bugs.cookie.set('autoLogin', "Y", oDate, "/","bugs.co.kr");
				
				jQuery('.layerAutoLogin').hide();
			});
		}
		*/
		
		submitButton.click(function(event) {
			bugs.login.submit(_form, userId, passWd);
		});
		//
		//jQuery("#frmLoginGnb button.btnLogin").click(function(event) {
		//	gnbLoginSubmit(_for, userId, passWd);
		//});
	}, 
	alert : function(text, option) {
		if(typeof bugs.ui.showAlert == "undefined") alert(text);
		else bugs.ui.showAlert(text, option);
	},
	empty : function(str) {
		if(typeof str == "string" && bugs.utils.trim(str).length > 0) 
			return false;
		return true;
	},
	
	submit : function(_form, userId, passWd) {
		if(typeof castType != "undefined" && castType=="live"){
			hideVideoLayer();
		}
		if(bugs.login.empty(userId.val())) {
			alert("아이디를 입력해주세요!");
			userId.get(0).focus();
			
			return false;
		}
		if(bugs.login.empty(passWd.val())) {
			alert("비밀번호를 입력해주세요!");
			passWd.get(0).focus();
			
			return false;
		}
		
		if(jQuery("#keepUid", _form).length > 0) {
			var oDate = new Date();
			if (jQuery("#keepUid", _form).is(":checked")) {
				oDate.setDate(oDate.getDate() + 30);	
				bugs.cookie.set("saveId", userId.val(), oDate, "/");
			} else {
				oDate.setDate(oDate.getDate() - 1);
				bugs.cookie.set("saveId", "", oDate, "/");
			}
		}
		var rUrl = jQuery("input[name=rUrl]", _form);
		if(bugs.login.empty(rUrl.val())) {
			//rUrl.val(g_urlBase);
			rUrl.val(document.location.href);
		}
		
		_form.submit();
	}
		
}